import { computed, onMounted } from "vue";
import { useListPage } from "@/composables/useListPage";
import stockApi, {
  type StockVerifyListItem,
  type StockVerifyListParams,
  type StockVerifyListResult
} from "@/api/system/verify/stock";

interface StockFilterForm {
  keyword: string;
  status: string | number;
}

const DEFAULT_FILTER: StockFilterForm = {
  keyword: "",
  status: ""
};

const STATUS_OPTIONS = [
  { label: "全部状态", value: "" },
  { label: "待核实", value: 0 },
  { label: "已核实", value: 1 },
  { label: "已拒绝", value: 2 },
  { label: "已取消", value: 3 }
];

const statusTagType = (status: number) => {
  switch (status) {
    case 1:
      return "success";
    case 2:
      return "danger";
    case 3:
      return "info";
    case 0:
    default:
      return "warning";
  }
};

const auditStatusLabel = (status: number) => {
  switch (status) {
    case 1:
      return "已通过";
    case 2:
      return "已驳回";
    default:
      return "待审核";
  }
};

const auditStatusTag = (status: number) => {
  switch (status) {
    case 1:
      return "success";
    case 2:
      return "danger";
    default:
      return "warning";
  }
};

const formatInteger = (value: number | null | undefined) => {
  if (value === null || value === undefined) return "-";
  return Number.isFinite(value) ? value.toLocaleString("zh-CN") : String(value);
};

export function useStockList() {
  const {
    formState: filterForm,
    pagination,
    dataList,
    loading,
    onSearch,
    resetForm,
    handlePageChange,
    handleSizeChange
  } = useListPage<StockFilterForm, StockVerifyListItem, StockVerifyListResult>(
    {
      fetch: (params: StockFilterForm & { page: number; page_size: number }) =>
        stockApi.fetchList(params as StockVerifyListParams)
    },
    DEFAULT_FILTER,
    {},
    {
      paramsAdapter: params => {
        const query: StockVerifyListParams = {
          page: params.page,
          page_size: params.page_size
        };

        const keyword = params.keyword?.trim();
        if (keyword) {
          query.keyword = keyword;
        }

        if (params.status !== "" && params.status !== undefined) {
          query.status = Number(params.status);
        }

        return query;
      },
      responseAdapter: (response: StockVerifyListResult) => {
        const paginationMeta = response?.pagination ?? {
          total: 0,
          pageSize: 10,
          currentPage: 1
        };

        return {
          list: response?.list ?? [],
          total: Number(paginationMeta.total ?? 0),
          pageSize: Number(paginationMeta.pageSize ?? 10),
          currentPage: Number(
            paginationMeta.currentPage ?? paginationMeta.page ?? 1
          )
        };
      }
    }
  );

  const tableData = computed(() => dataList.value ?? []);

  const columns: TableColumnList = [
    { label: "核实单号", prop: "verify_no", width: 180 },
    {
      label: "商品信息",
      prop: "product",
      minWidth: 220,
      align: "left",
      slot: "product"
    },
    {
      label: "买家需求",
      prop: "buyer",
      minWidth: 220,
      align: "left",
      slot: "buyer"
    },
    {
      label: "卖家反馈",
      prop: "seller",
      minWidth: 220,
      align: "left",
      slot: "seller"
    },
    {
      label: "审核信息",
      prop: "audit",
      minWidth: 220,
      align: "left",
      slot: "audit"
    },
    { label: "状态", prop: "status", width: 120, slot: "status" },
    { label: "更新时间", prop: "update_time", width: 180 }
  ];

  const handleSearch = () => {
    filterForm.page = 1;
    onSearch();
  };

  const handleResetFilters = () => {
    resetForm();
    onSearch();
  };

  onMounted(() => {
    onSearch();
  });

  return {
    filterForm,
    statusOptions: STATUS_OPTIONS,
    pagination,
    tableData,
    columns,
    loading,
    handleSearch,
    handleResetFilters,
    handlePageChange,
    handleSizeChange,
    statusTagType,
    formatInteger,
    auditStatusLabel,
    auditStatusTag
  };
}
